package com.autism.thread;

/**
 * 实现卖票任务
 */
public class RunnableImpl implements Runnable {

    //定义票源
    private int ticket = 100;
    Object object = new Object();

    //设置线程任务：卖票
    @Override
    public void run() {
        while (true) {

            //同步代码块
            synchronized (object) {
                if (ticket > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ":" + ticket + "张票");
                    ticket--;
                }
            }
        }
    }
}
